﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using AMS.DataAccess.BO;
using AMS.DataAccess.Common;

/// <summary>
/// Summary description for BaseWorkPage
/// </summary>
public class BaseWorkPage : PageBase
{
    public BaseWorkPage()
    {
    }

    private const string GuidKeyForMsgInfo = "F7B5AB5D-B484-47BF-BE5C-F30289A751EA";

    #region API for showing message
    /// <summary>
    /// 
    /// </summary>
    /// <param name="msg"></param>
    public void MsgInfo(string msg)
    {
        MsgInfo(msg, false);
    }

    /// <summary>
    /// 
    /// </summary>
    /// <param name="msg"></param>
    /// <param name="isAjaxCall"></param>
    public void MsgInfo(string msg, bool isAjaxCall)
    {
        ShowMsgInternal(GuidKeyForMsgInfo, "msgInfo", msg, isAjaxCall);
    }

    /// <summary>
    /// 
    /// </summary>
    /// <param name="msg"></param>
    public void MsgWarning(string msg)
    {
        MsgWarning(msg, false);
    }

    /// <summary>
    /// 
    /// </summary>
    /// <param name="msg"></param>
    /// <param name="isAjaxCall"></param>
    public void MsgWarning(string msg, bool isAjaxCall)
    {
        ShowMsgInternal(GuidKeyForMsgInfo, "msgWarning", msg, isAjaxCall);
    }


    /// <summary>
    /// 
    /// </summary>
    /// <param name="msg"></param>
    public void MsgError(string msg)
    {
        MsgError(msg, false);
    }

    /// <summary>
    /// 
    /// </summary>
    /// <param name="msg"></param>
    /// <param name="isAjaxCall"></param>
    public void MsgError(string msg, bool isAjaxCall)
    {
        ShowMsgInternal(GuidKeyForMsgInfo, "msgError", msg, isAjaxCall);
    }

    /// <summary>
    /// 
    /// </summary>
    /// <param name="key"></param>
    /// <param name="msgFun"></param>
    /// <param name="msg"></param>
    /// <param name="isAjaxCall"></param>
    private void ShowMsgInternal(string key, string msgFun, string msg, bool isAjaxCall)
    {
        if (isAjaxCall)
        {
            Anthem.Manager.AddScriptForClientSideEval(
                string.Format("{0}('{1}');",
                    msgFun,
                    Utils.EscapeSingleQuote(msg)));
        }
        else
        {
            ClientScript.RegisterClientScriptBlock(GetType(),
                key,
                string.Format("<script language='javascript'>{0}('{1}');</script>",
                    msgFun,
                    Utils.EscapeSingleQuote(msg)));
        }
    }
    #endregion


    #region API for parsing query string
    /// <summary>
    /// Parse string pareameter
    /// </summary>
    /// <param name="paramName"></param>
    /// <param name="outValue"></param>
    /// <returns></returns>
    protected bool ParseStringParameter(string paramName, out string outValue)
    {
        return ParseStringParameter(paramName, false, out outValue);
    }

    /// <summary>
    /// Parse string paramter.
    /// </summary>
    /// <param name="paramName"></param>
    /// <param name="allowNull"></param>
    /// <param name="outValue"></param>
    /// <returns></returns>
    protected bool ParseStringParameter(string paramName, bool allowNull, out string outValue)
    {
        outValue = Request.QueryString[paramName];
        if (string.IsNullOrEmpty(outValue))
        {
            return allowNull;
        }
        else
        {
            return true;
        }
    }

    /// <summary>
    /// 
    /// </summary>
    /// <param name="paramName"></param>
    /// <param name="outValue"></param>
    /// <returns></returns>
    protected bool ParseIntParameter(string paramName, out int outValue)
    {
        return ParseIntParameter(paramName, false, out outValue);
    }

    /// <summary>
    /// 
    /// </summary>
    /// <param name="paramName"></param>
    /// <param name="allowNull"></param>
    /// <param name="outValue"></param>
    /// <returns></returns>
    protected bool ParseIntParameter(string paramName, bool allowNull, out int outValue)
    {
        outValue = 0;
        if (!string.IsNullOrEmpty(Request.QueryString[paramName]))
        {
            return int.TryParse(Request.QueryString[paramName].Trim(), out outValue);
        }
        else
        {
            return (allowNull);
        }
    }

    /// <summary>
    /// Test if the parameter is in candidates
    /// </summary>
    /// <param name="value"></param>
    /// <param name="candidates"></param>
    /// <returns></returns>
    protected static bool ValidateParameter<T>(T value, T[] candidates)
    {
        if (candidates == null || candidates.Length == 0)
        {
            return true;
        }

        return 0 <= Array.IndexOf<T>(candidates, value);
    }
    #endregion

    protected CallContext CallContext
    {
        get
        {
            CallContext context = Session["CallContext"] as CallContext;
            if (context == null)
            {
                context = new CallContext();
                context.UserID = 1;
                context.UserName = "zyl";
                Session["CallContext"] = context;
            }

            return context;
        }
    }
}
